<template>
	<home-view v-if="isLogin"/>
	<login-view v-else/>
</template>
<script>
	import orderApi from '@/api/order.js';
	import merchantApi from '@/api/merchant.js';
	export default{
		name: 'Home',
		data() {
			return {
				isLogin: uni.getStorageSync('isLogin') || false
			}
		},
		watch: {
			isLogin: {
				immediate: true,
				handler(val) {
					this.setTabBar(val);
				}
			}
		},
		onLoad() {
			uni.$on('userLogin', async data => {
				if (data.isLogin) {
					const res = await merchantApi.findMyMerchant();
					const myMerchants = res.data.data;
					if (myMerchants.length === 1) {
						uni.setStorageSync('merchantId', myMerchants[0]._id);
					}
				}
				this.setIsLoginValue(data.isLogin);
			})	
		},
		methods: {
			setIsLoginValue(val) {
				uni.setStorage({
					key: 'isLogin',
					data: val
				})
				this.isLogin = val;
			},
			setTabBar(val) {
				if (val) {
					uni.showTabBar();
				} else {
					uni.hideTabBar();
				}
			}
		}
	}
</script>